﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ElingOA6_Admin_DSJCJ.Common;
using ElingOA6_Admin_DSJCJ.Models;

namespace ElingOA6_Admin_DSJCJ.Areas.Admin.Controllers
{
    [OutputCache(Duration = 0, VaryByParam = "none")]
    public class DeptShowTextController : AdminBaseController
    {
        //
        // GET: /Admin/DeptShowText/

        [AuthorityActionFilter("m_DeptShowText", Order = 1)]
        [LogPageActionFilter("部门显示信息", Order = 2)]
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult DeptTree(string focusNodeName = "")
        {
            BLL.HRDept deptBll = new BLL.HRDept();
            TreeModelList treeModelList = new TreeModelList();

            List<Model.HRDept> deptAllList = new List<Model.HRDept>();
            deptAllList = deptBll.GetListByEntityNo(GlobalConstant.ENTITYNO);
            if (deptAllList.Count > 0)
            {
                List<Model.HRDept> deptlist = (from c in deptAllList where c.FParentDeptNo == null & c.FIsAgency == "Y" orderby c.FSeq select c).ToList();

                foreach (Model.HRDept dept in deptlist)
                {
                    TreeItemModel treeParentItemModel = new TreeItemModel(null)
                    {
                        Id = dept.FDeptNo,
                        Name = dept.FDeptName,
                        ImageUrl = Url.Content("~/Content/ControlImages/dept_Groups.png")
                    };

                    List<Model.HRDept> deptModelList = (from c in deptAllList where c.FParentDeptNo == dept.FDeptNo & c.FIsAgency == "Y" orderby c.FSeq select c).ToList();
                    if (deptModelList.Count > 0)
                    {
                        GetChildDept(deptModelList, treeParentItemModel, deptAllList);
                    }

                    treeModelList.Add(treeParentItemModel);
                }

            }
            ViewData["focusNodeName"] = focusNodeName;
            return View(treeModelList);
        }

        /// <summary>
        /// 递归获取部门树
        /// </summary>
        /// <param name="deptChildList">部门列表</param>
        /// <param name="treeParentItemModel">父级树节点</param>
        /// <param name="deptAllList">部门集合</param>
        private void GetChildDept(List<Model.HRDept> deptChildList, TreeItemModel treeParentItemModel, List<Model.HRDept> deptAllList)
        {
            foreach (Model.HRDept dept in deptChildList)
            {

                TreeItemModel treeChildItemModel = new TreeItemModel(treeParentItemModel)
                {
                    Id = dept.FDeptNo,
                    Name = dept.FDeptName,
                    ImageUrl = Url.Content("~/Content/ControlImages/dept_Groups.png")
                };

                List<Model.HRDept> deptModelList = (from c in deptAllList where c.FParentDeptNo == dept.FDeptNo & c.FIsAgency == "Y" orderby c.FSeq select c).ToList();

                if (deptModelList.Count > 0)
                {
                    GetChildDept(deptModelList, treeChildItemModel, deptAllList);
                }

            }

        }

        public ActionResult DeptContent(string deptNo)
        {
            Model.HRDept dept = new Model.HRDept();
           
            if (null != deptNo)
            {
                deptNo = Fetch.estimateString(deptNo, CheckGetEnum.Safety);

                BLL.HRDept deptBll = new BLL.HRDept();
                dept = deptBll.GetModel(GlobalConstant.ENTITYNO, deptNo);

                if (null != dept)
                {
                    //获取显示信息
                    string ftpsowcontent = deptBll.GetFShowContent(GlobalConstant.ENTITYNO, deptNo);
                    ViewData["FShowContent"] = ftpsowcontent;
                    return View("DeptContent", dept);
                }
                else
                {
                    Response.Write("<script>alert('当前部门不存在');window.close();</script>");
                    Response.Flush();
                    Response.End();
                    return null;
                }
            }
            Response.Write("<script>alert('参数不正确');window.close();</script>");
            Response.Flush();
            Response.End();
            return null;
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult SavaModel(Model.HRDept entityModel)
        {
            //更新显示信息
            try
            {
                string entityNo = GlobalConstant.ENTITYNO;
                string ftpcontent = HttpUtility.HtmlDecode(Fetch.Post("FFtpShowText").Replace("\r\n",""));
                BLL.HRDept deptBll = new BLL.HRDept();
                deptBll.SetFShowContent(ftpcontent, GlobalConstant.ENTITYNO, entityModel.FDeptNo);
                ViewData["FShowContent"] = ftpcontent;
                ViewData["jsAlert"] = "保存成功";
                return View("DeptContent", entityModel);
            }
            catch (System.Exception ex)
            {
                ViewData["jsAlert"] = "错误代码:"+ex.Message.ToString();
                return View("DeptContent", new Model.HRDept());
            }
        }
    }
}
